package com.atguigu.auth.service.impl;

import com.atguigu.auth.mapper.SysRoleMapper;
import com.atguigu.auth.service.SysRoleService;
import com.atguigu.auth.service.SysUserRoleService;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysUserRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName: SysRoleService
 * Package: com.atguigu.auth.service.impl
 *
 * @Author: lmf
 * @Create: 2024/1/15 21:58
 * @Version: 1.0
 * Description:
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    private final SysUserRoleService userRoleService;

    public SysRoleServiceImpl(SysUserRoleService userRoleService) {
        this.userRoleService = userRoleService;
    }

    // 查询所有角色和用户所属角色
    @Override
    public Map<String, Object> findRoleDateByUserId(Long userid) {
        // 查询所有角色，返回list集合
        final List<SysRole> list = baseMapper.selectList(null);
        System.out.println(list + "===================");
        // 根据userid查询用户关系表查询userid对应所有角色id
        final LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        // 设置查询条件
        queryWrapper.eq(SysUserRole::getUserId, userid);

        //  从查询出来的用户id对应角色的list集合中，获取所有的角色id
        final List<SysUserRole> list1 = userRoleService.list(queryWrapper);
        // 根据查询到的所有角色id 找到对应的角色信息
        final List<Long> collect = list1.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        System.out.println(collect + "=====");
        // 根据角色id到所有的角色的list集合进行比较
        final ArrayList<SysRole> assignList = new ArrayList<>();
        for (SysRole sysRole : list) {
            if (collect.contains(sysRole.getId())) {
                // 将角色对象放入集合中
                assignList.add(sysRole);
            }
        }
        System.out.println(assignList);
        // 把得到的角色对象放入到map集合中
        final HashMap<String, Object> data = new HashMap<>();
        data.put("assignRoleList", assignList);
        data.put("AllRoleList", list);
        return data;
    }

    // 为用户分配角色
    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        // 把之前用户分配的角色数据删除，根据用户id删除
        final LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, assginRoleVo.getUserId());
        userRoleService.remove(queryWrapper);
        // 重新进行分配
        for (long roleId : assginRoleVo.getRoleIdList()) {
            if (StringUtils.isEmpty(roleId)) continue;
            final SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(assginRoleVo.getUserId());
            sysUserRole.setRoleId(roleId);
            userRoleService.save(sysUserRole);
        }
    }
}
